package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.MyException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: health_parent
 * @description:
 * @author: HuChunping
 * @create: 2021-01-08 20:09
 **/
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    /**
    * @Description: 新增检查组
    * @Param: [checkitemIds, checkGroup]
    * @return: void
    * @Author: HuChunping
    * @Date: 2021/1/9-17:11
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        // 向检查组当中添加
        checkGroupDao.add(checkGroup);
        // 涉及到中间表，插入表后，还要获取其id，(sql配置文件中实现),将id插入到中间表
        // 获取检查组的id
        Integer checkGroupId = checkGroup.getId();

        // 如果检查项的id数组不为空(前端传的数组，可能为空)
        if(null != checkitemIds) {
            for (Integer checkitemId : checkitemIds) {
                // 将检查组id，检查项id添加到中间表
                checkGroupDao.addCheckGroupCheckItem(checkGroupId, checkitemId);
            }
        }
    }

    /**
    * @Description: 分页查询
    * @Param: [queryPageBean]
    * @return: com.itheima.health.entity.PageResult<com.itheima.health.pojo.CheckGroup>
    * @Author: HuChunping
    * @Date: 2021/1/9-17:12
    */
    @Override
    public PageResult<CheckGroup> findPage(QueryPageBean queryPageBean) {
        // 前端传来的pageSize不能无限大，根据前端传参进行分页
        // 此处可限制：int queryPageBeanSize = queryPageBean.getPageSize() > 50?50:queryPageBean.getPageSize();
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        // 如果查询语句不为空，进行模糊查询
        if (StringUtil.isNotEmpty(queryPageBean.getQueryString())) {
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }

        // Page：com.github.pagehelper.Page，继承Arraylist,所以可以封装集合
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryPageBean.getQueryString());
        // 获取com.github.pagehelper.Page中的total(总数)与Result(结果)
        PageResult<CheckGroup> pageResult = new PageResult<CheckGroup>(page.getTotal(),page.getResult());
        return pageResult;
    }

    /**
    * @Description: 通过id查询检查组
    * @Param: [id]
    * @return: com.itheima.health.pojo.CheckGroup
    * @Author: HuChunping
    * @Date: 2021/1/9-17:12
    */
    @Override
    public CheckGroup findById(int id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    /**
    * @Description: 通过检查组id查询中间表获取勾选的检查项
    * @Param: [id]
    * @return: java.util.List<java.lang.Integer>
    * @Author: HuChunping
    * @Date: 2021/1/9-17:18
    */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(int id) {
        List<Integer> checkItemIds = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        return checkItemIds;
    }

    /**
    * @Description: 更新检查组
    * @Param: [checkitemIds, checkgroup]
    * @return: void
    * @Author: HuChunping
    * @Date: 2021/1/9-17:50
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Integer[] checkitemIds, CheckGroup checkGroup) {
        // 更新检查组
        checkGroupDao.update(checkGroup);

        // 删除旧关系(先直接根据检查组id删除所有关联的检查项检查项，然后再添加新关系)
        checkGroupDao.deleteCheckGroupCheckItem(checkGroup.getId());

        // 如果检查项不为空，添加检查项检查组中间表
        if (null != checkitemIds) {
            for (Integer checkitemId : checkitemIds) {
                // 循环添加检查组检查项
                checkGroupDao.addCheckGroupCheckItem(checkGroup.getId(), checkitemId);
            }
        }

    }

    /**
    * @Description: 通过id删除检查组
    * @Param: [id]
    * @return: void
    * @Author: HuChunping
    * @Date: 2021/1/10-17:10
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(int id) {
        int count = checkGroupDao.findCountByCheckGroupId(id);
        if (count > 0) {
            throw new MyException("检查组被套餐使用，不能删除");
        }
        checkGroupDao.deleteCheckGroupCheckItem(id);
        checkGroupDao.deleteById(id);
    }

    /**
    * @Description: 查询所有检查组
    * @Param: []
    * @return: java.util.List<com.itheima.health.pojo.CheckGroup>
    * @Author: HuChunping
    * @Date: 2021/1/10-17:10
    */
    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups = checkGroupDao.findAll();
        return checkGroups;
    }

}
